package com.stylefeng.guns.core.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class DateUtils {

	public final static String DATE_A = "yyyy-MM-dd";
	public final static String DATE_B = "yyyy-MM-dd HH:mm:ss";
	public final static String DATE_C = "yyyyMMddHHmmss";
	public final static String DATE_D = "yyyyMMdd-HHmmss-SS";
	public final static String DATE_E = "M月d日";
	public final static String DATE_F = "MM-dd";

	// 自动检测字符串形式然后转换
	public static Date strToDate(String dateStr) {
		Date date = null;
		SimpleDateFormat sdf = null;
		if (dateStr == null || dateStr.equals("")) {
			throw new RuntimeException("DateUtils.strToDate():" + dateStr);
		} else if (dateStr.indexOf(":") > 0) {
			sdf = new SimpleDateFormat(DATE_B);
		} else if (dateStr.indexOf("-") > 0) {
			sdf = new SimpleDateFormat(DATE_A);
		}
		try {
			if (sdf != null) {
				date = sdf.parse(dateStr);
			}
		} catch (Exception e) {
			throw new RuntimeException("DateUtils.strToDate():" + dateStr);
		}
		return date;
	}

	// 得到时间
	public static String getDateTime() {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_B);
		return sdf.format(new Date());
	}

	// 得到时间
	public static String date_c() {
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_C);
		return sdf.format(new Date());
	}

	// 普通的时间转字符串方法
	public static String getDate(Date date, String format) {
		if (date == null) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	// 去掉最后时间的小数点
	public static String changeDate(String date) {
		if (date.indexOf(".") != -1) {
			date = date.substring(0, date.indexOf("."));
		}
		return date;
	}

	// 普通的当前时间转字符串方法，格式为yyyy-MM-dd
	public static String getBeginNow() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date()) + " 00:00:00";
	}

	// 普通的当前时间转字符串方法，格式为yyyy-MM-dd
	public static String getEndNow() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date()) + " 23:59:59";
	}

	// 改变开始时间变成 2014-12-12 00:00:00
	public static String change_begindate_formate(String date) {
		if (date.indexOf(":") == -1) {
			return date + " 00:00:00";
		}
		return date;
	}

	// 改变结束时间变成 2014-12-12 23:59:59
	public static String change_enddate_formate(String date) {
		if (date.indexOf(":") == -1) {
			return date + " 23:59:59";
		}
		return date;
	}

	// 两日期相减变成秒，返回秒
	public static String getSecond(String first, String second) {
		return (strToDate(first).getTime() - strToDate(second).getTime())
				/ 1000 + "";
	}

	// 订单号
	public static String getOrderIdByTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String newDate = sdf.format(new Date());
		String result = "";
		Random random = new Random();
		for (int i = 0; i < 3; i++) {
			result += random.nextInt(10);
		}
		return newDate + result;
	}

}
